<?php
namespace App\Http\Controllers\Admin;


use App\Models\Admin;
use App\Models\ChannelSource;
use App\Models\Location;
use App\Models\Menu;
use App\Models\Order;
use App\Models\Role;
use Illuminate\Http\Request;

/**
 * banner
 *
 * @author Miluo Huang
 */
class GlobalController extends Controller
{

	/**
	 * 单点删除
	 */
	public function individalDeletion(Request $request)
	{
		// 验证数据。
		$this->validate($request, [
			'id' => [
				'required',
			],
			'type' => [
				'required',
			]
		],[
			'id.required' => '请选择删除对象',
			'type.required' => '请选择删除类型',
		]);

		switch($request->input('type')){
			case 'admin':
				$data = Admin::find($request->input('id'));
				if(!is_null($data)){
					if($data->username=='root'){
						return redirect()->back()->withMessageError('删除失败,root用户不能删除.');
					}
					$data->delete();
				}
				break;
			case 'order':
				$data = Order::find($request->input('id'));
				if(!is_null($data)){
					$data->delete();
				}
				break;
			case 'channel':
				$data = ChannelSource::find($request->input('id'));
				if(!is_null($data)){
					$data->delete();
				}
			break;
			case 'menu':
				$data = Menu::find($request->input('id'));
				if(!is_null($data)){
					$data->delete();
				}
				break;
			case 'role':
				$data = Role::find($request->input('id'));
				if(!is_null($data)){
					$data->delete();
				}
				break;
		}

		return redirect()->back()->withMessageSuccess('删除成功。');
	}

	/**
	 * 省市区数据
	 */
	public function getLocation(Request $request){
		$data = new Location();
		if($request->has('pid')){
			$data = $data->where('pid',$request->input('pid'));
		}
		return [
			'message' => 'success',
			'data' => $data->get()->toArray()
		];
	}
}
